<?php

class usraccount_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = "usr_account";
        $this->tableNameQuePas = "que_pass";
    }

    function insert($mpa_id_alumni, $username, $password, $accountemail, $phone,
        $twitterid, $facebookid, $openid, $gmailid, $locked) {
        $dt = array(
            "mpa_id_alumni" => $mpa_id_alumni,
            "ua_username" => $username,
            "ua_password" => $password,
            "ua_email" => $accountemail,
            "ua_phone" => $phone,
            "ua_twitter_id" => $twitterid,
            "ua_facebook_id" => $facebookid,
            "ua_open_id" => $openid,
            "ua_google_id" => $gmailid,
            "ua_islocked" => $locked);
        
        $this->db->insert($this->tableName, $dt);
        return $this->db->insert_id();
    }

    function insert_que_pas($username, $password, $accountemail) {
        $dt = array("username" => $username,
            "email" => $accountemail,
            "password" => $password);
        $this->db->insert($this->tableNameQuePas, $dt);
        return $this->db->insert_id();
    }

    function update($mpa_id_alumni, $dt, $where = "") {
        $this->db->where("mpa_id_alumni", $mpa_id_alumni);
        if ($where != "")
            $this->db->where($where);
        $this->db->update($this->tableName, $dt);
    }

    function delete($mpa_id_alumni, $where = "") {
        $this->db->where("mpa_id_alumni", $mpa_id_alumni);
        if ($where != "")
            $this->db->where($where);
        $this->db->delete($this->tableName);
    }

    function getById($mpa_id_alumni, $where = "") {
        $this->db->where("mpa_id_alumni", $mpa_id_alumni);
        if ($where != "")
            $this->db->where($where);
        return $this->db->get($this->tableName)->row();
    }

    function getAll($startpage = "0", $perpage = "10", $where = "", $orderby = "") {
        if ($where != "")
            $this->db->where($where);
        $this->db->limit($perpage, $startpage);
        if ($orderby != "")
            $this->db->order_by($orderby);
        return $this->db->get($this->tableName)->result();
    }

    function getAllCount($where = "") {
        $this->db->select("COUNT(*) JUMLAH");
        $this->db->from($this->tableName);
        if ($where != "")
            $this->db->where($where);
        return $this->db->get()->row()->JUMLAH;
    }

    function login($email, $pass) {
        //$this->db->select("*");
        //return $this->db->where(array("accountemail" => $email, "password" => md5($pass)))->get($this->tableName)->row();
        return $this->db->where(array("ua_email" => $email, "ua_password" => md5($pass), "ua_islocked" => 0))->get($this->tableName)->row();
    }

}

?>